package com.ncui.wisdomLearn.biz.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ncui.wisdomLearn.api.dto.RoleDTO;
import com.ncui.wisdomLearn.api.entity.Role;
import com.ncui.wisdomLearn.api.entity.UserRole;
import com.ncui.wisdomLearn.api.vo.RoleVO;
import com.ncui.wisdomLearn.biz.user.mapper.RoleMapper;
import com.ncui.wisdomLearn.biz.user.mapper.UserRoleMapper;
import com.ncui.wisdomLearn.biz.user.service.RoleService;
import com.ncui.wisdomLearn.common.exception.BizException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色表(Role)表 服务实现类
 *
 * @author lvlele
 * @since 2025-01-03 13:53:20
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    private final RoleMapper roleMapper;

    private final UserRoleMapper userRoleMapper;

    @Override
    public boolean addRole(RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        return save(role);
    }

    @Override
    public boolean updateRole(RoleDTO roleDTO) {
        if (roleDTO.getRoleId() == null) {
            throw new BizException("角色ID不能为空");
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        return updateById(role);
    }

    @Override
    public boolean deleteRole(Long roleId) {
        if (roleId == null) {
            throw new BizException("角色ID不能为空");
        }
        // 删除角色时同时删除用户-角色关联关系
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getRoleId, roleId);
        userRoleMapper.delete(wrapper);
        return removeById(roleId);
    }

    @Override
    public RoleVO getRoleById(Long roleId) {
        if (roleId == null) {
            throw new BizException("角色ID不能为空");
        }
        Role role = getById(roleId);
        if (role == null) {
            return null;
        }
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role, roleVO);
        return roleVO;
    }

    @Override
    public List<RoleVO> listRolesByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        // 根据ID列表查询角色
        List<Role> roles = roleMapper.selectByIds(ids);
        // 转换为VO对象
        return BeanUtil.copyToList(roles, RoleVO.class);
    }

    @Override
    public List<RoleVO> listAllRoles() {
        List<Role> roles = roleMapper.selectList(Wrappers.lambdaQuery(Role.class).orderByAsc(Role::getRoleId));
        return BeanUtil.copyToList(roles, RoleVO.class);
    }
}

